package com.test.learnkotlin.test

import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.flow
import kotlinx.coroutines.flow.onCompletion
import kotlinx.coroutines.flow.onStart
import kotlinx.coroutines.launch
import kotlinx.coroutines.runBlocking

fun main() = runBlocking {
    val TWO_SECONDS = 2_000L
    val word = "Thank"
    val len = word.length
    val TOTAL_TIME = len * TWO_SECONDS * 10

    GlobalScope.launch {
        flow {
            word.forEach {
                emit(it)
                delay(TWO_SECONDS)
            }
        }.onStart {

        }.onCompletion {
            println("\nend")
        }.collect {
            print(it)
        }
    }

    delay(TOTAL_TIME)
}

